{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "#Loading the image using OpenCV\n",
    "import cv2\n",
    "image= cv2.imread(\"test_image.jpg\")\n",
    "cv2.imshow(\"Input Image\",image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Converting Image to Grayscale\n",
    "import numpy as np\n",
    "lanelines_image=np.copy(image)\n",
    "gray_conversion = cv2.cvtColor(lanelines_image,cv2.COLOR_BGR2GRAY)\n",
    "cv2.imshow(\"Input Image\",gray_conversion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Smoothing the image\n",
    "blur_conversion=cv2.GaussianBlur(gray_conversion,(5,5),0)\n",
    "cv2.imshow(\"Input Image\",blur_conversion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Canny Edge Detection\n",
    "canny_conversion = cv2.Canny(blur_conversion,50,155)\n",
    "cv2.imshow(\"Input Image\",canny_conversion)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Masking the Region of Interest\n",
    "import matplotlib.pyplot as plt\n",
    "def reg_of_interest(image):\n",
    "    image_height=image.shape[0]\n",
    "    polygons=np.array([[(200,image_height),(1100,image_height),(550,250)]])\n",
    "    image_mask=np.zeros_like(image)\n",
    "    cv2.fillPoly(image_mask,polygons,255)\n",
    "    return image_mask\n",
    "cv2.imshow(\"Result\",reg_of_interest(canny_conversion))\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Applying Bitwise_and\n",
    "def reg_of_interest(image):\n",
    "    image_height=image.shape[0]\n",
    "    polygons=np.array([[(200,image_height),(1100,image_height),(550,250)]])\n",
    "    image_mask=np.zeros_like(image)\n",
    "    cv2.fillPoly(image_mask,polygons,255)\n",
    "    masking_image=cv2.bitwise_and(image,image_mask)\n",
    "    return masking_image\n",
    "cropped_image=reg_of_interest(canny_conversion)\n",
    "cv2.imshow(\"Result\",cropped_image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Applying the Hough Transform\n",
    "def show_lines(image,lines):\n",
    "    lines_image=np.zeros_like(image)\n",
    "    if lines is not None:\n",
    "        for line in lines:\n",
    "            X1,Y1,X2,Y2=line.reshape(4)\n",
    "            cv2.line(lines_image,(X1,Y1),(X2,Y2),(255,0,0),10)\n",
    "    return lines_image\n",
    "lanelines=np.copy(image)\n",
    "lane_lines=cv2.HoughLinesP(cropped_image,2,np.pi/180,100,np.array([]),minLineLength=40,maxLineGap=5)\n",
    "\n",
    "line_image=show_lines(lanelines_image,lane_lines)\n",
    "cv2.imshow(\"Result\",line_image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Show the results in original image\n",
    "combine_image=cv2.addWeighted(lanelines,0.8,line_image,1,1)\n",
    "cv2.imshow(\"Result\",combine_image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "def make_coordinates(image, line_parameters):\n",
    "          slope, intercept = line_parameters\n",
    "          y1 = image.shape[0]\n",
    "          y2 = int(y1*(3/5))\n",
    "          x1 = int((y1- intercept)/slope)\n",
    "          x2 = int((y2 - intercept)/slope)\n",
    "          return np.array([x1, y1, x2, y2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "def average_slope_intercept(image, lines):\n",
    "          left_fit = []\n",
    "          right_fit = []\n",
    "          for line in lines:\n",
    "            x1, y1, x2, y2 = line.reshape(4)\n",
    "            parameter = np.polyfit((x1, x2), (y1, y2), 1)\n",
    "            slope = parameter[0]\n",
    "            intercept = parameter[1]\n",
    "            if slope < 0:\n",
    "              left_fit.append((slope, intercept))\n",
    "            else:\n",
    "              right_fit.append((slope, intercept))\n",
    "          left_fit_average =np.average(left_fit, axis=0)\n",
    "          right_fit_average = np.average(right_fit, axis =0)\n",
    "          left_line =make_coordinates(image, left_fit_average)\n",
    "          right_line = make_coordinates(image, right_fit_average)\n",
    "  \n",
    "          return np.array([left_line, right_line])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def canny_edge(image):\n",
    "         gray_coversion= cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n",
    "         blur_conversion = cv2.GaussianBlur(gray_conversion, (5,5),0)\n",
    "         canny_conversion = cv2.Canny(blur_conversion, 50,150)\n",
    "         return canny_conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_lines(image, lines):\n",
    "          lanelines_image = np.zeros_like(image)\n",
    "          if lines is not None:\n",
    "            for line in lines:\n",
    "              X1, Y1, X2, Y2 = line.reshape(4)\n",
    "              cv2.line(lanelines_image, (X1, Y1), (X2, Y2), (255,0,0), 10)\n",
    "          return lanelines_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reg_of_interest(image):\n",
    "          image_height = image.shape[0]\n",
    "          polygons = np.array([[(200, image_height), (1100, image_height), (551, 250)]])\n",
    "          image_mask = np.zeros_like(image)\n",
    "          cv2.fillPoly(image_mask, polygons, 255)\n",
    "          masking_image = cv2.bitwise_and(image,image_mask)\n",
    "          return masking_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "image = cv2.imread('test_image.jpg')\n",
    "lanelines_image = np.copy(image)\n",
    "canny_image = canny_edge(lanelines_image)\n",
    "cropped_image = reg_of_interest(canny_image)\n",
    "lines = cv2.HoughLinesP(cropped_image, 2, np.pi/180, 100, np.array([]), minLineLength= 40, maxLineGap=5)\n",
    "averaged_lines = average_slope_intercept(lanelines_image, lines)\n",
    "line_image = show_lines(lanelines_image, averaged_lines)\n",
    "combine_image = cv2.addWeighted(lanelines_image, 0.8, line_image, 1, 1)\n",
    "cv2.imshow('result', combine_image)\n",
    "cv2.waitKey(0)\n",
    "cv2.destroyAllWindows()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Detecting Road Marking in a video"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "def make_coordinates(image, line_parameters):\n",
    "          try:\n",
    "            slope, intercept = line_parameters\n",
    "          except TypeError:\n",
    "            slope, intercept = 0.001,0\n",
    "          #slope, intercept = line_parameters\n",
    "          y1 = image.shape[0]\n",
    "          y2 = int(y1*(3/5))\n",
    "          x1 = int((y1- intercept)/slope)\n",
    "          x2 = int((y2 - intercept)/slope)\n",
    "          return np.array([x1, y1, x2, y2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "def average_slope_intercept(image, lines):\n",
    "          left_fit = []\n",
    "          right_fit = []\n",
    "          for line in lines:\n",
    "            x1, y1, x2, y2 = line.reshape(4)\n",
    "            parameter = np.polyfit((x1, x2), (y1, y2), 1)\n",
    "            slope = parameter[0]\n",
    "            intercept = parameter[1]\n",
    "            if slope < 0:\n",
    "              left_fit.append((slope, intercept))\n",
    "            else:\n",
    "              right_fit.append((slope, intercept))\n",
    "          left_fit_average =np.average(left_fit, axis=0)\n",
    "          right_fit_average = np.average(right_fit, axis =0)\n",
    "          left_line =make_coordinates(image, left_fit_average)\n",
    "          right_line = make_coordinates(image, right_fit_average)\n",
    "  \n",
    "          return np.array([left_line, right_line])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "def canny_edge(image):\n",
    "         gray_conversion= cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)\n",
    "         blur_conversion = cv2.GaussianBlur(gray_conversion, (5,5),0)\n",
    "         canny_conversion = cv2.Canny(blur_conversion, 50,150)\n",
    "         return canny_conversion"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "\n",
    "def show_lines(image, lines):\n",
    "          line_image = np.zeros_like(image)\n",
    "          if lines is not None:\n",
    "            for line in lines:\n",
    "              x1, y1, x2, y2 = line.reshape(4)\n",
    "              cv2.line(line_image, (x1, y1), (x2, y2), (255,0,0), 10)\n",
    "          return line_image\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "def reg_of_interest(image):\n",
    "          image_height = image.shape[0]\n",
    "          polygons = np.array([[(200, image_height), (1100, image_height), (550, 250)]])\n",
    "          image_mask = np.zeros_like(image)\n",
    "          cv2.fillPoly(image_mask, polygons, 255)\n",
    "          masking_image = cv2.bitwise_and(image,image_mask)\n",
    "          return masking_image"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "cap = cv2.VideoCapture(\"test2.mp4\")\n",
    "\n",
    "fourcc = cv2.VideoWriter_fourcc(*'mp4v') \n",
    "fps = int(cap.get(cv2.CAP_PROP_FPS))     \n",
    "width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))\n",
    "height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))\n",
    "out = cv2.VideoWriter('output.mp4', fourcc, fps, (width, height))\n",
    "\n",
    "while cap.isOpened():\n",
    "    ret, frame = cap.read()\n",
    "    if not ret:\n",
    "        break\n",
    "    \n",
    "    canny_image = canny_edge(frame)\n",
    "    cropped_canny = reg_of_interest(canny_image)\n",
    "    lines = cv2.HoughLinesP(cropped_canny, 2, np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5)\n",
    "    averaged_lines = average_slope_intercept(frame, lines)\n",
    "    line_image = show_lines(frame, averaged_lines)\n",
    "    combo_image = cv2.addWeighted(frame, 0.8, line_image, 1, 1)\n",
    "    \n",
    "    out.write(combo_image)\n",
    "\n",
    "    cv2.imshow(\"result\", combo_image)\n",
    "    if cv2.waitKey(1) & 0xFF == ord('q'):\n",
    "        break\n",
    "\n",
    "cap.release()\n",
    "out.release()\n",
    "cv2.destroyAllWindows()\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
